/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-09-06 11:13:30
 * @LastEditors: sueRimn
 * @LastEditTime: 2022-09-30 15:18:13
 */
const state = {
  visitedViews: [],//顶部导航数组
  cachedViews: []//缓存页面数组
}
const mutations = {
  ADD_VISITED_VIEW: (state, view) => {
    if (state.visitedViews.some(v => v.path === view.path)) return
    state.visitedViews.push(
      Object.assign({}, view, {
        title: view.meta.title || 'no-name'
      })
    )
  },
  ADD_CACHED_VIEW: (state, view) => {
    if (state.cachedViews.some(v => v.path === view.path)) return
    state.cachedViews.push(
      Object.assign({}, view, {
        title: view.meta.title || 'no-name'
      })
    )
  },
  DEL_CACHED_VIEW: (state, view) => {
    for (let [i, v] of state.cachedViews.entries()) {
      if (v.path == view.path) {
        state.cachedViews.splice(i, 1);
      }
    }
  },
  DEL_VISITED_VIEW: (state, view) => {
    for (let [i, v] of state.visitedViews.entries()) {
      if (v.path == view.path) {
        state.visitedViews.splice(i, 1);
      }
    }
  },
}
const actions = {
  addVisitedView({ commit }, view) {
    commit('ADD_VISITED_VIEW', view)
  },
  delVisitedView({ commit }, view) {
    commit('DEL_VISITED_VIEW', view)
  },
  addCachedView({ commit }, view) {
    commit('ADD_CACHED_VIEW', view)
  },
  delCachedView({ commit }, view) {
    commit('DEL_CACHED_VIEW', view)
  },
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}